package com.tianqi.ui.gui;

import com.tianqi.ui.service.Service;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ListModel;

import static javax.swing.JFrame.EXIT_ON_CLOSE;

public class MainGui {

    private Service service = new Service();

    private JPanel contentPanel = new JPanel();
    private DefaultListModel<String> result;

    private JLabel lableCity;
    private JTextField searchCity;
    private JButton buttonQuery;
    private JScrollPane scrollPane;
    private JList listInfo;

    public MainGui() {
        JFrame jf = new JFrame("天气小灵通");
        jf.setDefaultCloseOperation(EXIT_ON_CLOSE);
        jf.setBounds(200,200,1000,800);
        jf.setContentPane(contentPanel);
        jf.setLayout(null);

        //检索框
        lableCity = new JLabel("城市：");
        lableCity.setBounds(30,30,100,30);
        Font font = new Font(null,Font.BOLD,28);
        lableCity.setFont(font);
        contentPanel.add(lableCity);

        // 文本框
        searchCity = new JTextField();
        searchCity.setBounds(120,30,200,30);
        searchCity.setFont(font);
        contentPanel.add(searchCity);

        buttonQuery = new JButton("查询");
        buttonQuery.setBounds(350,30,100,30);
        buttonQuery.setFont(font);
        buttonQuery.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                result.removeAllElements();
                String city =searchCity.getText();
                result = service.query(city);
                System.out.println(city);
                listInfo.setModel(result);

            }
        });
        contentPanel.add(buttonQuery);

        listInfo = new JList<>();
        listInfo.setBounds(0,0,990,600);
        listInfo.setFont(font);
        scrollPane = new JScrollPane(listInfo);
        scrollPane.setBounds(5,90,960,600);
        contentPanel.add(scrollPane);

        result = service.query(null);
        listInfo.setModel(result);
        jf.setVisible(true);
    }

    public static void main(String[] args) {
        new MainGui();

    }

}
